/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Loggin.java
 *
 * Created on 10/06/2011, 08:03:14 PM
 */

package GUI.ATTENDANT;

import Procedures.DBConection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author SIX-14
 */
public class Loggin extends javax.swing.JFrame {

    /** Creates new form Loggin */
    private DBConection myConection;
    private JFrame loggin;
    public Loggin() {
        initComponents();
        
        setMiConexion(new DBConection("localhost", "systemloancic","root", "123456"));
    }
    public Loggin(Splash splash) {

        loggin=this;
        initComponents();
        setMiConexion(new DBConection("localhost", "systemloancic","root", "123456"));
        accessPanel1.getUser();
        accessPanel1.getPassword();
        accessPanel1.setNote("Rediseñado por marco");
        accessPanel1.getButton().addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
                if(loggingAtendant(accessPanel1.getUser(), accessPanel1.getPassword())){
            MainWindow mw= new MainWindow(getMyConection());
            loggin.setVisible(false);
            mw.setVisible(true);
        }else{
            JOptionPane.showMessageDialog(loggin,
                 "Compruebe que los datos ingresados sean correctos.",
                 "Compruebe los Datos",
                 JOptionPane.ERROR_MESSAGE);
        }
        jButtonPannel(evt);
    }
});
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMiConexion(DBConection miConexion) {
        this.myConection = miConexion;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    public boolean loggingAtendant(String username, String password){
        boolean loggintoken=false;
        if(username.isEmpty()||password.isEmpty()){
            loggintoken=false;
        }else{
            getMyConection().sendSQLQuery("SELECT * FROM atendant WHERE " +
            "username = '" + username + "' && password = '" + password + "';");
            try{
                if (getMyConection().getResultado().next()) {
                    loggintoken=true;
                }else{
                    loggintoken=false;
                }
            }catch (SQLException ex) {
            Logger.getLogger(Loggin.class.getName()).log(Level.SEVERE, null, ex);
            }catch (Exception e){
                System.out.println(e.getMessage());
            }

        }

        return loggintoken;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        accessPanel1 = new ingenieriaproyectos.guis.AccessPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Loguear Encargado");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(accessPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(accessPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void jButtonPannel(java.awt.event.MouseEvent evt) {
        JOptionPane.showMessageDialog(this, "Mensaje");
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Loggin().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ingenieriaproyectos.guis.AccessPanel accessPanel1;
    // End of variables declaration//GEN-END:variables

}
